﻿using System;

namespace CSharp进阶_匿名函数
{
	class Test
	{
		public Action action;
		public Test()
		{
			int localValue = 10;
			action = () =>
			{
				Console.WriteLine("! " + localValue);
			};
			
			localValue = 20;
			for (int i = 0; i < 10; i++)
			{
				action += () =>
				{
					Console.WriteLine("@ " + i);
				};
			}
			for (int i = 0; i < 10; i++)
			{
				int index = i;
				action += () =>
				{
					Console.WriteLine("# " + index);
				};
			}
		}
	}
	class Program
	{
		static void Main(string[] args)
		{
			Test t = new Test();
			t.action();

			//Console.WriteLine(Function(5)(6));
		}
		static Func<int, int> Function(int a)
		{
			// 这里改变了a的生命周期，匿名函数将会捕获a
			return delegate(int b)
			{
				Console.WriteLine("{0} + {1} = {2}", a, b, a+b);
				return a+b;
			};
		}
	}
}
